<?php

namespace app\admin\model\insurance;

use think\Model;


class Policy extends Model
{

    

    

    // 表名
    protected $name = 'insurance_policy';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'startTime_text',
        'endTime_text',
        'insured_type_text',
        'time_text'
    ];
    

    public function getInsuredTypeList()
    {
        return [0 => '无对应类型', 11 => '投标保证保险', 12 => '投标保函', 21=> '采购合同履约保证保险', 22 => '售后履约(保证金)保证保险', 23 => '履约保函', 24 => '售后履约保函', 61 => '采购合同预付款保证保险', 62 => '预付款保函',80 => '商城增信保函'];
    }
    public function getInsuredTypeTextAttr($value, $data)
    {
        $list =  $this->getInsuredTypeList();
        $value = $value ? $value : (isset($data['insuredType']) ? $data['insuredType'] : 0);
        return isset($list[$value]) ? $list[$value] : '';
    }



    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['startTime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['endTime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    
    public function customers()
    {
        return $this->hasMany('Customer', 'order_id', 'id');
    }
    public function dictionary()
    {
        return $this->hasOne('\\app\\admin\\model\\Dictionary', 'code', 'distCode');
    }


}
